#include <iostream>
#include <cstring>

using namespace std;

class Solution
{
public:
    static void replaceSpace(char *str,int length)
    {
        int blankCount = 0;
        int originLength = 0;
        while(str[originLength] != '\0')
        {
            if(str[originLength] == ' ')
                blankCount++;
            originLength++;
        }

        int resaultStrLength = originLength + blankCount*2;
        if(resaultStrLength > length)
            return;

        int currStartIndex = 0;
        int currEndIndex = originLength;
        while(currStartIndex<resaultStrLength)
        {
            cout<<currStartIndex<<endl;
            if(str[currStartIndex] == ' ')
            {
                for(int i = currEndIndex; i> currStartIndex; i--)
                    str[i+2] = str[i];
                currEndIndex += 2;

                str[currStartIndex] = '%';
                str[++currStartIndex] = '2';
                str[++currStartIndex] = '0';
            }
            else
                currStartIndex++;
        }
        str[currStartIndex+1] = '\0';
    }
     static void replaceSpace2(char *str,int length)
    {
        int blankCount = 0;
        int originLength = 0;
        while(str[originLength] != '\0')
        {
            if(str[originLength] == ' ')
                blankCount++;
            originLength++;
        }

        int resaultStrLength = originLength + blankCount*2;
        if(resaultStrLength > length)
            return;
        int index1 = originLength;
        int index2 = resaultStrLength;

        while(index1 != index2 && index1>=0)
        {
            cout<<index1<<endl;
            if(str[index1] != ' ')
            {
                str[index2] = str[index1];
                index2--;
                index1--;
            }
            else
            {
                index1 --;
                str[index2] = '0';
                str[--index2] = '2';
                str[--index2] = '%';
            }
        }
        str[resaultStrLength+1] = '\0';

    }
};

int main()
{
    char *str = new char[100];
    char *tempStr = "We Are Happy.";
    strcpy(str,tempStr);
    Solution::replaceSpace2(str,100);
    cout<<str<<endl;
    Solution::replaceSpace(str,100);
    cout<<str<<endl;

}
